<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/1/2
 * Time: 9:37
 */

namespace app\api\controller;

use app\common\model\SkillApply;
use GatewayWorker\Lib\Gateway;

class Events extends Base
{

    protected static $user_id;

    /**
     * 有消息时
     * @param integer $client_id 连接的客户端
     * @param mixed $message
     * @return void
     */
    public static function onMessage($client_id, $message)
    {
        $msg = json_decode($message,true);
        var_dump($msg);
        if (!$msg){
            self::clientResponse(-1,$client_id,'登录过期');
        }else{
            $user_id = Gateway::getUidByClientId($client_id);
            if (!$user_id){
                if (!isset($msg['token'])) self::clientResponse(-1,$client_id,'登录过期');
                $user_id = self::getUserId($msg['token']);
                if (!$user_id) self::clientResponse(-1,$client_id,'登录过期');
                Gateway::bindUid($client_id,$user_id);
            }
            self::$user_id = $user_id;
        }

        $check = dehashid($msg['group_id']);

        if (!is_numeric($check)) self::userResponse(0,self::$user_id,'参数错误');

        switch ($msg['type']){
            case 'join': //加入群组
                var_dump('in:');
                Gateway::joinGroup($client_id,$msg['group_id']);
                self::refresh($msg['group_id']);
                break;
            case 'leave': //退出群组
                Gateway::leaveGroup($client_id,$msg['group_id']);
                self::refresh($msg['group_id']);
                break;
            default :
                self::userResponse(0,self::$user_id,'访问类型错误');
                break;
        }
        return;
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @param $group_id
     * 刷新群组内用户
     */
    protected static function refresh($group_id)
    {
        var_dump($group_id);
        if (!isset($group_id) || empty($group_id)){
            self::userResponse(0,self::$user_id,'群组id获取不到');
            return;
        }
        var_dump('进入:');
        $data = Gateway::getUidListByGroup($group_id);

        $orderCondition = cache('fastOrder_'.$group_id);

        if (is_numeric($orderCondition['user_id'])){
            $user_id = $orderCondition['user_id'];
        }else{
            $user_id = dehashid($group_id);
        }

        $master = Base::staticInfo('header_img,nick_name,user_id',$user_id);
        $master['user_id'] = hashid($master['user_id']);
        $rows = [];
        if ($data){
            //邀约条件

            $model = new SkillApply();
            $map['e.online_status'] = 1;
            $map['a.user_id']       = ['in',$data];
//        $map['a.skill_id']      = $orderCondition['skill_id'];
            $map['e.invite']        = 1;

            $rows = $model->getFastInfo($map);

            cache('userGroup_'.self::$user_id,$group_id);
        }

        return self::groupResponse(200,$group_id,'刷新群组角色',['master'=>$master,'rows'=>$rows]);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 向连接者发送消息
     */
    protected static function clientResponse($code,$client_id = null,$msg = '请求成功',$data = []){

        $response['code'] = $code;
        $response['msg']  = $msg;
        $response['data'] = $data;
        $json = json_encode($response)."\r\n\r\n";

        if (!$client_id || $client_id =='all'){
            return Gateway::sendToAll($json);
        }else{
            return Gateway::sendToClient($client_id,$json);
        }

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 单独向客户端发送消息
     */
    protected static function userResponse($code,$user_id = null,$msg,$data = [])
    {
        $response['code'] = $code;
        $response['msg']  = $msg;
        $response['data'] = $data;
        $json = json_encode($response)."\r\n\r\n";

        if (!$user_id || $user_id = 'all'){
            return Gateway::sendToAll($json);
        }else{
            return Gateway::sendToUid($user_id,$json);
        }
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 向群组发送消息
     */
    public static function groupResponse($code,$group_id,$msg = '服务端消息推送',$data = [])
    {
        $response['code'] = $code;
        $response['msg']  = $msg;
        $response['data'] = $data;
        $json = json_encode($response)."\r\n\r\n";

        return Gateway::sendToGroup($group_id,$json);

    }




    /**
     * 当用户连接时触发的方法
     * @param integer $client_id 连接的客户端
     * @return void
     */
    public static function onConnect($client_id)
    {

        $response['code'] = 110;
        $response['msg']  = '连接成功';
        $response['data'] = [];
        $json = json_encode($response)."\r\n\r\n";

        return Gateway::sendToClient($client_id,$json);
    }

    /**
     * 当用户断开连接时触发的方法
     * @param integer $client_id 断开连接的客户端
     * @return void
     */
    public static function onClose($client_id)
    {
        $group_id = cache('userGroup_'.self::$user_id);
        self::refresh($group_id);
        Gateway::unbindUid($client_id,self::$user_id);
        return self::clientResponse(1,$client_id,'退出成功');
    }

    /**
     * 当进程启动时
     * @param integer $businessWorker 进程实例
     */
    public static function onWorkerStart($businessWorker)
    {
        echo "WorkerStart1\r\n\r\n";
    }

    /**
     * 当进程关闭时
     * @param integer $businessWorker 进程实例
     */
    public static function onWorkerStop($businessWorker)
    {
        echo "WorkerStop\r\n\r\n";
    }
}